package com.yjt.fresh.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpVpProductDetail;
import com.yjt.fresh.api.ApiAddBuyCart;
import com.yjt.fresh.api.ApiAddOrDeleteCollection;
import com.yjt.fresh.api.ApiGetCartList;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanCart;
import com.yjt.fresh.bean.BeanCartChange;
import com.yjt.fresh.bean.BeanProductDetail;
import com.yjt.fresh.bean.MessageEvent;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.dialog.CustomViewBottomDialog;
import com.yjt.fresh.fragment.FrgProductDetailComment;
import com.yjt.fresh.fragment.FrgProductDetailDetail;
import com.yjt.fresh.fragment.FrgProductDetailProduct;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToast;
import com.yjt.fresh.widget.CustomToastResult;
import com.yjt.fresh.widget.WarpLinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 商品详情
 * Created by UPC on 2018/6/15.
 */

public class AtyProductDetail extends BaseSimpleActivity {
    private static final int REQUEST_CODE = 1008;
    @BindView(R.id.vp_app_aty_product_detail)
    ViewPager mVp;
    @BindView(R.id.tvAppAtyProductDetailGoods)
    TextView mTvGoods;
    @BindView(R.id.vAppAtyProductDetailGoods)
    View mVGoods;
    @BindView(R.id.rlAppAtyProductDetailGoods)
    RelativeLayout mRlGoods;
    @BindView(R.id.tvAppAtyProductDetailGoodsDetail)
    TextView mTvGoodsDetail;
    @BindView(R.id.vAppAtyProductDetailGoodsDetail)
    View mVGoodsDetail;
    @BindView(R.id.rlAppAtyProductDetailGoodsDetail)
    RelativeLayout mRlGoodsDetail;
    @BindView(R.id.tvAppAtyProductDetailComment)
    TextView mTvComment;
    @BindView(R.id.vAppAtyProductDetailComment)
    View mVComment;
    @BindView(R.id.rlAppAtyProductDetailComment)
    RelativeLayout mRlComment;
    @BindView(R.id.ivAppAtyProductDetailShare)
    ImageView mIvShare;
    @BindView(R.id.ivAppAtyProductDetailCollect)
    ImageView mIvCollect;
    @BindView(R.id.tvAppAtyProductDetailStore)
    TextView mTvStore;
    @BindView(R.id.tvAppAtyProductDetailService)
    TextView mTvService;
    @BindView(R.id.tvAppAtyProductDetailCart)
    TextView mTvCart;
    @BindView(R.id.tvAppAtyProductDetailCartCount)
    TextView mTvCartCount;
    @BindView(R.id.tvAppAtyProductDetailJoinCart)
    TextView mTvJoinCart;
    @BindView(R.id.tvAppAtyProductDetailBuy)
    TextView mTvBuy;
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.rlAppAtyProductDetailCartCount)
    RelativeLayout mRlCartCount;


    private final int PRODUCT = 0;
    private final int DETAIL = 1;
    private final int COMMENT = 2;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static final int REQUEST_PERMISSION_CODE = 1008;

    private BeanProductDetail mBeanProductDetail;
    private String mTypeOneSelectPosition;
    private String mTypeTwoSelectPosition;
    private String mTypeOne;
    private String mTypeTwo;
    private int mGoodsStock;
    private int mGoodsCount = 1;
    private CustomViewBottomDialog mBottomDialog;
    private TextView mTvChooseTypeNewPrice;
    private ImageView mIvChooseTypeClose;
    private TextView mTvTypeOne;
    private WarpLinearLayout mWlTypeOne;
    private TextView mTvTypeTwo;
    private WarpLinearLayout mWlTypeTwo;
    private ImageView mIvSub;
    private TextView mTvGoodsCount;
    private ImageView mIvAdd;
    private ImageView mIvGoodsLogo;
    private TextView mTvChooseTypeOldPrice;
    private TextView mTvDialogAddToCart;
    private TextView mTvDialogBuy;
    private boolean isCollect;
    private String mChildGoodsID;
    private TextView mTvChildGoodsType;
    private FrgProductDetailProduct mFrgProduct;
    private int mCartCount;


    @Override
    protected int getContentView() {
        return R.layout.app_aty_product_detail;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initVP();
        initClick();
        EventBus.getDefault().register(this);
        getData();
    }

    HttpOnNextListener<List<BeanCart>> mGetCartListener = new HttpOnNextListener<List<BeanCart>>() {
        @Override
        public void onNext(List<BeanCart> beanCarts) {
            if (beanCarts != null && beanCarts.size() > 0) {
                mRlCartCount.setVisibility(View.VISIBLE);
                mCartCount = beanCarts.size();
                mTvCartCount.setText(beanCarts.size() + "");
            } else {
                mRlCartCount.setVisibility(View.GONE);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void getData() {
        if (PreferenceManager.getInstance().getIsLogin()) {
            ApiGetCartList apiGetCartList = new ApiGetCartList(mGetCartListener, mContext, true, false, false);
            HashMap<String, String> map = new HashMap<>();
            map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
            apiGetCartList.setMap(map);
            HttpManager.getInstance().doHttpDeal(apiGetCartList);
        }
    }

    /*EventBus-----------------------------------*/
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent data) {
        if (data.getRecode() == MyConstants.TO_PRODUCT_COMMENT) {
            if (mVp != null) {
                mVp.setCurrentItem(COMMENT);
            }
        }
        if (data.getRecode() == MyConstants.CHOOSE_GOODS_TYPE) {
            showChooseTypeView();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BeanProductDetail beanProductDetail) {
        mBeanProductDetail = beanProductDetail;
        if ("1".equals(mBeanProductDetail.getIS_COLLECT())) {
            mIvCollect.setImageResource(R.mipmap.icon_collected);
            isCollect = true;
        } else {
            mIvCollect.setImageResource(R.mipmap.b4_nav_ic_02);
            isCollect = false;
        }

        if ("1".equals(mBeanProductDetail.getChildgoodsList().get(0).getGOODS_SPEC_TYPE())) {
            mChildGoodsID = mBeanProductDetail.getChildgoodsList().get(0).getCHILDGOODS_ID();
        }
    }
    /*EventBus-----------------------------------*/

    private void showChooseTypeView() {
        if (mBottomDialog == null) {
            mBottomDialog = new CustomViewBottomDialog(mContext);
            initDialog();
            mBottomDialog.builder();
        }

        mBottomDialog.show();
    }

    private void initDialog() {

        initDialogView();
        initDialogClick();
    }

    private void initDialogClick() {
        mIvChooseTypeClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBottomDialog != null) {
                    mBottomDialog.dismiss();
                }
            }
        });

        mTvDialogAddToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mChildGoodsID != null) {
                    mBottomDialog.dismiss();
                    addGoods2Cart();
                } else {
                    Toast.makeText(mContext, "请选择规格", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mTvDialogBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mChildGoodsID != null) {
                    mBottomDialog.dismiss();
                    buyRightAway();
                } else {
                    Toast.makeText(mContext, "请选择规格", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mIvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGoodsStock == 0) {
                    CustomToast.showLong("请先选择规格");
                    return;
                }
                if (mGoodsCount <= mGoodsStock) {
                    mGoodsCount++;
                    mTvGoodsCount.setText(mGoodsCount + "");
                } else {
                    CustomToast.showLong("购买数量不能大于库存量");
                }
            }
        });

        mIvSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGoodsCount > 1) {
                    mGoodsCount--;
                    mTvGoodsCount.setText(mGoodsCount + "");
                }
            }
        });
    }

    private void initDialogView() {
        View inflate = View.inflate(mContext, R.layout.app_dialog_goods_choose, null);
        mTvChooseTypeNewPrice = inflate.findViewById(R.id.tvAppAtyProductDetailChooseTypeNewPrice);
        mTvChooseTypeOldPrice = inflate.findViewById(R.id.tvAppAtyProductDetailChooseTypeOldPrice);
        mIvChooseTypeClose = inflate.findViewById(R.id.ivAppAtyProductDetailChooseTypeClose);
        mTvTypeOne = inflate.findViewById(R.id.tvAppAtyProductDetailChooseTypeTypeOne);
        mWlTypeOne = inflate.findViewById(R.id.wlAppAtyProductDetailChooseTypeTypeOne);
        mTvTypeTwo = inflate.findViewById(R.id.tvAppAtyProductDetailChooseTypeTypeTwo);
        mWlTypeTwo = inflate.findViewById(R.id.wlAppAtyProductDetailChooseTypeTypeTwo);
        mIvSub = inflate.findViewById(R.id.ivAppAtyProductDetailChooseTypeSub);
        mTvGoodsCount = inflate.findViewById(R.id.tvAppAtyProductDetailChooseTypeGoodsCount);
        mIvAdd = inflate.findViewById(R.id.ivAppAtyProductDetailChooseTypeAdd);
        mIvGoodsLogo = inflate.findViewById(R.id.ivAppAtyProductDetailChooseTypeGoodsLogo);
        mTvDialogAddToCart = inflate.findViewById(R.id.tvAppDialogChooseProductTypeAddToCart);
        mTvDialogBuy = inflate.findViewById(R.id.tvAppDialogChooseProductTypeBuy);
        mTvChildGoodsType = inflate.findViewById(R.id.tvAppAtyProductDetailChooseTypeTypeDetail);
        mBottomDialog.setView(inflate);
        BeanProductDetail.ChildgoodsListBean childgoodsListBean = mBeanProductDetail.getChildgoodsList().get(0);
        mTvChooseTypeNewPrice.setText("¥ " + childgoodsListBean.getSINGLE_PRICE() + "");
        mTvChooseTypeOldPrice.setText("¥ " + mBeanProductDetail.getDISPLAY_PRICE());
        mTvGoodsCount.setText("1");
        mTvChooseTypeOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        Glide.with(mContext).load(childgoodsListBean.getSKU_IMG()).into(mIvGoodsLogo);
        if ("1".equals(childgoodsListBean.getGOODS_SPEC_TYPE())) {//单品
            mChildGoodsID = childgoodsListBean.getCHILDGOODS_ID();
        } else if ("2".equals(childgoodsListBean.getGOODS_SPEC_TYPE())) {//一种规格
            mTvTypeOne.setText(childgoodsListBean.getONE_SPEC_TYPE());
            mTvTypeTwo.setVisibility(View.GONE);
            mWlTypeTwo.setVisibility(View.GONE);
            oneType(childgoodsListBean);
        } else if ("3".equals(childgoodsListBean.getGOODS_SPEC_TYPE())) {
            mTvTypeOne.setText(childgoodsListBean.getONE_SPEC_TYPE());
            mTvTypeTwo.setText(childgoodsListBean.getTWO_SPEC_TYPE());
            twoType();
        }
    }

    private void twoType() {
        final ArrayList<String> listTypeOne = new ArrayList<>();
        final ArrayList<String> listTypeTwo = new ArrayList<>();
        final ArrayList<String> listTypeOneAndTwo = new ArrayList<>();
        final List<BeanProductDetail.ChildgoodsListBean> childgoodsList = mBeanProductDetail.getChildgoodsList();
        for (int i = 0; i < childgoodsList.size(); i++) {
            BeanProductDetail.ChildgoodsListBean childGoods = childgoodsList.get(i);
            if (!listTypeOne.contains(childGoods.getONE_SPEC_CONTENT())) {
                listTypeOne.add(childGoods.getONE_SPEC_CONTENT());
            }
            if (!listTypeTwo.contains(childGoods.getTWO_SPEC_CONTENT())) {
                listTypeTwo.add(childGoods.getTWO_SPEC_CONTENT());
            }
            if (!listTypeOneAndTwo.contains(childGoods.getONE_SPEC_CONTENT() + "," + childGoods.getTWO_SPEC_CONTENT())) {
                listTypeOneAndTwo.add(childGoods.getONE_SPEC_CONTENT() + "," + childGoods.getTWO_SPEC_CONTENT());
            }
        }

        for (int i = 0; i < listTypeOne.size(); i++) {
            final TextView tv = (TextView) View.inflate(mContext, R.layout.app_warpll_item_search_history, null);
            tv.setText(listTypeOne.get(i));
            tv.setBackgroundResource(R.drawable.shape_cccccc_border_5);
            mWlTypeOne.addView(tv);
        }

        for (int i = 0; i < listTypeTwo.size(); i++) {
            final TextView tv = (TextView) View.inflate(mContext, R.layout.app_warpll_item_search_history, null);
            tv.setText(listTypeTwo.get(i));
            tv.setBackgroundResource(R.drawable.shape_cccccc_border_5);
            mWlTypeTwo.addView(tv);
        }

        for (int i = 0; i < mWlTypeOne.getChildCount(); i++) {
            final Position positionTypeOne = new Position();
            final TextView childView = (TextView) mWlTypeOne.getChildAt(i);
            positionTypeOne.position = i;
            childView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //再次点击 取消
                    if (mTypeOneSelectPosition != null && positionTypeOne.position == Integer.valueOf(mTypeOneSelectPosition)) {
                        //两种规格都已经选择
                        if (mTypeTwo != null) {
                            for (int j = 0; j < listTypeOne.size(); j++) {
                                if (listTypeOneAndTwo.contains(listTypeOne.get(j) + "," + mTypeTwo)) {
                                    mWlTypeOne.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                    mWlTypeOne.getChildAt(j).setClickable(true);
                                } else {
                                    mWlTypeOne.getChildAt(j).setBackgroundResource(R.drawable.shape_e6e6e6_5);
                                    mWlTypeOne.getChildAt(j).setClickable(false);
                                }
                            }

                            for (int j = 0; j < listTypeTwo.size(); j++) {
                                if (!(j + "").equals(mTypeTwoSelectPosition)) {
                                    mWlTypeTwo.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                    mWlTypeTwo.getChildAt(j).setClickable(true);
                                }
                            }
                            //重置 参数
                            mTypeOne = null;
                            mGoodsCount = 1;
                            mTvGoodsCount.setText("1");
                            mChildGoodsID = null;
                            Glide.with(mContext).load(childgoodsList.get(0).getSKU_IMG()).into(mIvGoodsLogo);
                            mTypeOneSelectPosition = null;
                            mGoodsStock = 0;
                            mTvChildGoodsType.setText("请选择规格");
                            mFrgProduct.setGoodsType("请选择规格");

                        } else {
                            for (int j = 0; j < listTypeOne.size(); j++) {
                                mWlTypeOne.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                mWlTypeOne.getChildAt(j).setClickable(true);
                            }
                            for (int j = 0; j < listTypeTwo.size(); j++) {
                                mWlTypeTwo.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                mWlTypeTwo.getChildAt(j).setClickable(true);
                            }
                            mTypeOne = null;
                            mTypeTwo = null;
                            mGoodsCount = 1;
                            mTvGoodsCount.setText("1");
                            mChildGoodsID = null;
                            Glide.with(mContext).load(childgoodsList.get(0).getSKU_IMG()).into(mIvGoodsLogo);
                            mTypeOneSelectPosition = null;
                            mTypeTwoSelectPosition = null;
                            mGoodsStock = 0;
                            mTvChildGoodsType.setText("请选择规格");
                            mFrgProduct.setGoodsType("请选择规格");
                        }
                    } else {
                        if (mTypeOneSelectPosition != null) {
                            Integer typeOneSelectPosition = Integer.valueOf(mTypeOneSelectPosition);
                            mWlTypeOne.getChildAt(typeOneSelectPosition).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                        }
                        mTypeOneSelectPosition = positionTypeOne.position + "";
                        childView.setBackgroundResource(R.drawable.shape_ff2b2b_border_5);
                        mTypeOne = childView.getText().toString();
                        if (mTypeTwo != null) {
                            for (int j = 0; j < childgoodsList.size(); j++) {
                                if (mTypeOne.equals(childgoodsList.get(j).getONE_SPEC_CONTENT()) && mTypeTwo.equals(childgoodsList.get(j).getTWO_SPEC_CONTENT())) {
                                    mGoodsStock = Integer.valueOf(childgoodsList.get(j).getSTOCK());
                                    Glide.with(mContext).load(childgoodsList.get(j).getSKU_IMG()).into(mIvGoodsLogo);
                                    mTvChooseTypeNewPrice.setText("¥ " + childgoodsList.get(j).getSINGLE_PRICE());
                                    mChildGoodsID = childgoodsList.get(j).getCHILDGOODS_ID();
                                    mTvChildGoodsType.setText("已选规格: "+mTypeOne + "," + mTypeTwo);
                                    mFrgProduct.setGoodsType("已选规格: "+mTypeOne + "," + mTypeTwo);
                                }
                            }
                        }
                        for (int j = 0; j < listTypeTwo.size(); j++) {
                            if (listTypeOneAndTwo.contains(mTypeOne + "," + listTypeTwo.get(j))) {
                                if (!(j + "").equals(mTypeTwoSelectPosition)) {
                                    mWlTypeTwo.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                }
                                mWlTypeTwo.getChildAt(j).setClickable(true);
                            } else {
                                mWlTypeTwo.getChildAt(j).setBackgroundResource(R.drawable.shape_e6e6e6_5);
                                mWlTypeTwo.getChildAt(j).setClickable(false);
                            }
                        }
                    }
                }
            });
        }

        for (int i = 0; i < mWlTypeTwo.getChildCount(); i++) {
            final Position positionTypeTwo = new Position();
            final TextView childView = (TextView) mWlTypeTwo.getChildAt(i);
            positionTypeTwo.position = i;
            childView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mTypeTwoSelectPosition != null && positionTypeTwo.position == Integer.valueOf(mTypeTwoSelectPosition)) {
                        if (mTypeOne != null) {
                            for (int j = 0; j < listTypeTwo.size(); j++) {
                                if (listTypeOneAndTwo.contains(mTypeOne + "," + listTypeTwo.get(j))) {
                                    mWlTypeTwo.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                    mWlTypeTwo.getChildAt(j).setClickable(true);
                                } else {
                                    mWlTypeTwo.getChildAt(j).setBackgroundResource(R.drawable.shape_e6e6e6_5);
                                    mWlTypeTwo.getChildAt(j).setClickable(false);
                                }
                            }
                            for (int j = 0; j < listTypeOne.size(); j++) {
                                if (!(j + "").equals(mTypeOneSelectPosition)) {
                                    mWlTypeOne.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                    mWlTypeOne.getChildAt(j).setClickable(true);
                                }
                            }
                            mTypeTwo = null;
                            mGoodsCount = 1;
                            mTvGoodsCount.setText("1");
                            mChildGoodsID = null;
                            Glide.with(mContext).load(childgoodsList.get(0).getSKU_IMG()).into(mIvGoodsLogo);
                            mTypeTwoSelectPosition = null;
                            mGoodsStock = 0;
                            mTvChildGoodsType.setText("请选择规格");
                            mFrgProduct.setGoodsType("请选择规格");

                        } else {
                            for (int j = 0; j < listTypeTwo.size(); j++) {
                                mWlTypeTwo.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                mWlTypeTwo.getChildAt(j).setClickable(true);
                            }
                            for (int j = 0; j < listTypeOne.size(); j++) {
                                mWlTypeOne.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                mWlTypeOne.getChildAt(j).setClickable(true);
                            }
                            mTypeOne = null;
                            mTypeTwo = null;
                            mGoodsCount = 1;
                            mTvGoodsCount.setText("1");
                            mChildGoodsID = null;
                            Glide.with(mContext).load(childgoodsList.get(0).getSKU_IMG()).into(mIvGoodsLogo);
                            mTypeOneSelectPosition = null;
                            mTypeTwoSelectPosition = null;
                            mGoodsStock = 0;
                            mTvChildGoodsType.setText("请选择规格");
                            mFrgProduct.setGoodsType("请选择规格");
                        }
                    } else {
                        if (mTypeTwoSelectPosition != null) {
                            Integer typeOneSelectPosition = Integer.valueOf(mTypeTwoSelectPosition);
                            mWlTypeTwo.getChildAt(typeOneSelectPosition).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                        }
                        mTypeTwoSelectPosition = positionTypeTwo.position + "";
                        childView.setBackgroundResource(R.drawable.shape_ff2b2b_border_5);
                        mTypeTwo = childView.getText().toString();
                        if (mTypeOne != null) {
                            for (int j = 0; j < childgoodsList.size(); j++) {
                                if (mTypeOne.equals(childgoodsList.get(j).getONE_SPEC_CONTENT()) && mTypeTwo.equals(childgoodsList.get(j).getTWO_SPEC_CONTENT())) {
                                    mGoodsStock = Integer.valueOf(childgoodsList.get(j).getSTOCK());
                                    Glide.with(mContext).load(childgoodsList.get(j).getSKU_IMG()).into(mIvGoodsLogo);
                                    mTvChooseTypeNewPrice.setText("¥ " + childgoodsList.get(j).getSINGLE_PRICE());
                                    mChildGoodsID = childgoodsList.get(j).getCHILDGOODS_ID();
                                    mTvChildGoodsType.setText("已选规格: "+mTypeOne + "," + mTypeTwo);
                                    mFrgProduct.setGoodsType("已选规格: "+mTypeOne + "," + mTypeTwo);
                                }
                            }
                        }
                        for (int j = 0; j < listTypeOne.size(); j++) {
                            if (listTypeOneAndTwo.contains(listTypeOne.get(j) + "," + mTypeTwo)) {
                                if (!(j + "").equals(mTypeOneSelectPosition)) {
                                    mWlTypeOne.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                                }
                                mWlTypeOne.getChildAt(j).setClickable(true);
                            } else {
                                mWlTypeOne.getChildAt(j).setBackgroundResource(R.drawable.shape_e6e6e6_5);
                                mWlTypeOne.getChildAt(j).setClickable(false);
                            }
                        }
                    }
                }
            });
        }
    }


    private void oneType(BeanProductDetail.ChildgoodsListBean childgoodsListBean) {
        mTvTypeTwo.setVisibility(View.GONE);
        mTvTypeTwo.setVisibility(View.GONE);
        mTvTypeOne.setText(childgoodsListBean.getONE_SPEC_TYPE());
        final List<BeanProductDetail.ChildgoodsListBean> childgoodsList = mBeanProductDetail.getChildgoodsList();
        for (int i = 0; i < childgoodsList.size(); i++) {
            final Position position = new Position();
            position.position = i;
            final TextView tv = (TextView) View.inflate(mContext, R.layout.app_warpll_item_search_history, null);
            tv.setText(childgoodsList.get(i).getONE_SPEC_CONTENT());
            tv.setBackgroundResource(R.drawable.shape_cccccc_border_5);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BeanProductDetail.ChildgoodsListBean childGoods = childgoodsList.get(position.position);
                    Glide.with(mContext).load(childGoods.getSKU_IMG()).into(mIvGoodsLogo);
                    for (int j = 0; j < mWlTypeOne.getChildCount(); j++) {
                        mWlTypeOne.getChildAt(j).setBackgroundResource(R.drawable.shape_cccccc_border_5);
                    }
                    if (!childGoods.getCHILDGOODS_ID().equals(mChildGoodsID)) {
                        tv.setBackgroundResource(R.drawable.shape_ff2b2b_border_5);
                        mGoodsStock = Integer.valueOf(childGoods.getSTOCK());
                        mChildGoodsID = childGoods.getCHILDGOODS_ID();
                        mTvChildGoodsType.setText("已选择:  " + childGoods.getONE_SPEC_CONTENT());
                        mFrgProduct.setGoodsType("已选择:  " + childGoods.getONE_SPEC_CONTENT());
                    } else {
                        mChildGoodsID = null;
                        mTvChildGoodsType.setText("请选择规格");
                        mFrgProduct.setGoodsType("请选择规格");
                        mGoodsCount = 1;
                        mTvGoodsCount.setText("1");
                    }
                }
            });
            mWlTypeOne.addView(tv);
        }
    }

    private void initClick() {
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mIvCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceManager.getInstance().getIsLogin()) {
                    addOrDeleteCollect();
                } else {
                    toLogin();
                }
            }
        });
        mRlGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsIsSelected();
                mVp.setCurrentItem(PRODUCT);
            }
        });

        mRlComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commentIsSelected();

                mVp.setCurrentItem(COMMENT);
            }
        });

        mRlGoodsDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsDetailIsSelected();
                mVp.setCurrentItem(DETAIL);
            }
        });

        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case PRODUCT:
                        goodsIsSelected();
                        break;
                    case DETAIL:
                        goodsDetailIsSelected();
                        break;
                    case COMMENT:
                        commentIsSelected();
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mIvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(mContext, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
                    } else {
                        share();
                    }
                } else {
                    share();
                }
            }
        });
        mTvJoinCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mChildGoodsID != null) {
                    addGoods2Cart();
                } else {
                    showChooseTypeView();
                }
            }
        });

        mTvBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mChildGoodsID != null) {
                    buyRightAway();
                } else {
                    showChooseTypeView();
                }
            }
        });

        mTvCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceManager.getInstance().getIsLogin()) {
                    Intent intent = new Intent(mContext, AtyCart.class);
                    startActivityForResult(intent, REQUEST_CODE);
                } else {
                    Intent intent = new Intent(mContext, AtyLogin.class);
                    startActivity(intent);
                }
            }
        });

        mTvStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setRecode(MyConstants.TO_STORE);
                EventBus.getDefault().post(messageEvent);
            }
        });

        mTvService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callPhoneDialog();
            }
        });
    }

    private void share() {
//        UMWeb umWeb = new UMWeb("http://healthmeat.lanmeihulian.com/healthmeat/" + mBeanProductDetail.getSHAREGOODSDETAIL());
        UMWeb umWeb = new UMWeb("http://jiankangshengxian.com/healthmeat/" + mBeanProductDetail.getSHAREGOODSDETAIL());
        UMImage umImage = new UMImage(mContext, R.mipmap.ic_launcher);
        umWeb.setTitle("健康肉");
        umWeb.setDescription(mBeanProductDetail.getTITLE());
        umWeb.setThumb(umImage);
        new ShareAction(mContext)
                .withMedia(umWeb)
                .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        CustomToastResult.show("分享成功");
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        Toast.makeText(mContext, throwable.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {

                        Toast.makeText(mContext, "分享取消", Toast.LENGTH_SHORT).show();
                    }
                }).open();
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_CODE) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i("MainActivity", "申请的权限为：" + permissions[i] + ",申请结果：" + grantResults[i]);
            }
        }
    }

    private void buyRightAway() {
        if (PreferenceManager.getInstance().getIsLogin()) {


            List<BeanCartChange> list = new ArrayList<>();
            BeanCartChange beanCartChange = new BeanCartChange();
            beanCartChange.setStoreName(mBeanProductDetail.getSTORE_NAME());
            List<BeanCart> carts = new ArrayList<>();
            BeanCart beanCart = new BeanCart();

            List<BeanProductDetail.ChildgoodsListBean> childList = mBeanProductDetail.getChildgoodsList();
            for (int i = 0; i < childList.size(); i++) {
                BeanProductDetail.ChildgoodsListBean child = childList.get(i);
                if (mChildGoodsID.equals(child.getCHILDGOODS_ID())) {
                    beanCart.setCHILDGOODS_ID(mChildGoodsID);
                    beanCart.setCOUNT(mGoodsCount);
                    beanCart.setCREATE_TIME(child.getCREATE_TIME());
                    beanCart.setSTORE_NAME(mBeanProductDetail.getSTORE_NAME());
                    beanCart.setSTORE_ID(child.getSTORE_ID());
                    BeanCart.GOODINFOBean goodinfoBean = new BeanCart.GOODINFOBean();
                    goodinfoBean.setSINGLE_PRICE(child.getSINGLE_PRICE());
                    goodinfoBean.setCREATE_TIME(child.getCREATE_TIME());
                    goodinfoBean.setGOODS_STATUS(Integer.valueOf(child.getGOODS_STATUS()));
                    goodinfoBean.setGOODS_FREIGHT_MONEY(Double.valueOf(child.getGOODS_FREIGHT_MONEY()));
                    goodinfoBean.setIS_DELETE(Integer.valueOf(child.getIS_DELETE()));
                    goodinfoBean.setSTOCK(Integer.valueOf(child.getSTOCK()));
                    goodinfoBean.setSTORE_ID(child.getSTORE_ID());
                    goodinfoBean.setONE_SPEC_CONTENT(child.getONE_SPEC_CONTENT());
                    goodinfoBean.setSKU_IMG(child.getSKU_IMG());
                    goodinfoBean.setONE_SPEC_TYPE(child.getONE_SPEC_TYPE());
                    goodinfoBean.setGOODS_AUDIT_STATUS(Integer.valueOf(child.getGOODS_AUDIT_STATUS()));
                    goodinfoBean.setGOODS_SPEC_TYPE(Integer.valueOf(child.getGOODS_SPEC_TYPE()));
                    goodinfoBean.setCHILDGOODS_ID(child.getCHILDGOODS_ID());
                    goodinfoBean.setGOODS_LIMIT_BUY(Integer.valueOf(child.getGOODS_LIMIT_BUY()));
                    goodinfoBean.setTWO_SPEC_CONTENT(child.getTWO_SPEC_CONTENT());
                    goodinfoBean.setGOODS_IS_DELETE(Integer.valueOf(child.getGOODS_IS_DELETE()));
                    goodinfoBean.setTWO_SPEC_TYPE(child.getTWO_SPEC_TYPE());
                    goodinfoBean.setGOODS_ID(child.getGOODS_ID());
                    goodinfoBean.setTITLE(mBeanProductDetail.getTITLE());
                    beanCart.setGOODINFO(goodinfoBean);
                }
            }
            carts.add(beanCart);
            beanCartChange.setBeanCarts(carts);
            list.add(beanCartChange);
            EventBus.getDefault().postSticky(list);
            Intent intent = new Intent(mContext, AtyBalance.class);
            startActivity(intent);
        } else {
            toLogin();
        }

    }

    HttpOnNextListener<BaseResultEntity> mCollectListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (isCollect) {
                CustomToastResult.show("取消成功");
                mIvCollect.setImageResource(R.mipmap.b4_nav_ic_02);
            } else {
                CustomToastResult.show("收藏成功");
                mIvCollect.setImageResource(R.mipmap.icon_collected);
            }
            isCollect = !isCollect;
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void addOrDeleteCollect() {
        ApiAddOrDeleteCollection apiAddOrDeleteCollection = new ApiAddOrDeleteCollection(mCollectListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("DETAIL_ID", mBeanProductDetail.getGOODS_ID());
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiAddOrDeleteCollection.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiAddOrDeleteCollection);
    }

    HttpOnNextListener<BaseResultEntity> mAddBuyCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (mBottomDialog != null) {
                mBottomDialog.dismiss();
            }
            mRlCartCount.setVisibility(View.VISIBLE);
            mTvCartCount.setText((mCartCount + mGoodsCount) + "");
            CustomToastResult.show("加入购物车成功");
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void addGoods2Cart() {
        if (PreferenceManager.getInstance().getIsLogin()) {

            ApiAddBuyCart apiAddBuyCart = new ApiAddBuyCart(mAddBuyCartListener, mContext, true, false, false);
            Map<String, Object> map = new HashMap<>();
            map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
            map.put("COUNT", mGoodsCount);
            map.put("CHILDGOODS_ID", mChildGoodsID);
            apiAddBuyCart.setMap(map);
            HttpManager.getInstance().doHttpDeal(apiAddBuyCart);
        } else {
            toLogin();
        }
    }

    private void goodsDetailIsSelected() {
        mTvGoodsDetail.setTextColor(getResources().getColor(R.color.color_43b624));
        mVGoodsDetail.setVisibility(View.VISIBLE);

        mTvGoods.setTextColor(getResources().getColor(R.color.color_333333));
        mVGoods.setVisibility(View.GONE);
        mTvComment.setTextColor(getResources().getColor(R.color.color_333333));
        mVComment.setVisibility(View.GONE);
    }

    private void commentIsSelected() {
        mTvComment.setTextColor(getResources().getColor(R.color.color_43b624));
        mVComment.setVisibility(View.VISIBLE);

        mTvGoods.setTextColor(getResources().getColor(R.color.color_333333));
        mVGoods.setVisibility(View.GONE);
        mTvGoodsDetail.setTextColor(getResources().getColor(R.color.color_333333));
        mVGoodsDetail.setVisibility(View.GONE);
    }

    private void goodsIsSelected() {
        mTvGoods.setTextColor(getResources().getColor(R.color.color_43b624));
        mVGoods.setVisibility(View.VISIBLE);

        mTvComment.setTextColor(getResources().getColor(R.color.color_333333));
        mVComment.setVisibility(View.GONE);
        mTvGoodsDetail.setTextColor(getResources().getColor(R.color.color_333333));
        mVGoodsDetail.setVisibility(View.GONE);
    }

    private void initVP() {
        String goodsID = getIntent().getStringExtra(MyConstants.GOODS_ID);
        Bundle bundle = new Bundle();
        bundle.putString(MyConstants.GOODS_ID, goodsID);
        List<Fragment> fragments = new ArrayList<>();
        mFrgProduct = new FrgProductDetailProduct();
        mFrgProduct.setArguments(bundle);
        FrgProductDetailComment frgComment = new FrgProductDetailComment();
        frgComment.setArguments(bundle);
        FrgProductDetailDetail frgDetail = new FrgProductDetailDetail();
        fragments.add(mFrgProduct);
        fragments.add(frgDetail);
        fragments.add(frgComment);
        AdpVpProductDetail adpVpProductDetail = new AdpVpProductDetail(getSupportFragmentManager(), fragments);
        mVp.setAdapter(adpVpProductDetail);
    }

    private void callPhoneDialog() {
        final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
        dialog.setContentView(R.layout.app_dialog_sure_cancel);
        TextView tvTitle = dialog.findViewById(R.id.tvAppDIalogSureCancelTitle);
        TextView tvContent = dialog.findViewById(R.id.tvAppDIalogSureCancelContent);
        TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
        TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("即将拨通客服电话哦");
        tvContent.setText(mBeanProductDetail.getSTORE_TEL());
        tvCancel.setText("取消");
        tvSure.setText("立即拨打");
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMobileDialerInterface(mBeanProductDetail.getSTORE_TEL());
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private class Position {
        public int position;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST_CODE == requestCode) {
            getData();
        }
        UMShareAPI.get(mContext).onActivityResult(requestCode, resultCode, data);
    }
}
